home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / STRSTREA.CC < prev    next >
C/C++ Source or Header  |  1992-03-30  |  6KB  |  240 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1991 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #ifdef __GNUG__
  19. #pragma implementation "strstream.h"
  20. #endif
  21. #include "ioprivate.h"
  22. #include <strstream.h>
  23.  
  24. istrstream::istrstream(char *cp) : istream(NULL)
  25. {
  26.     _strbuf = new strstreambuf(cp, 0, NULL);
  27. }
  28.  
  29. istrstream::istrstream(char *cp, int n) : istream(NULL)
  30. {
  31.     _strbuf = new strstreambuf(cp, n, NULL);
  32. }
  33.  
  34. ostrstream::ostrstream() : ostream(NULL)
  35. {
  36.     _strbuf = new strstreambuf;
  37. }
  38.  
  39. ostrstream::ostrstream(char *cp, int n, int mode) : ostream(NULL)
  40. {
  41.     char *pstart;
  42.     if (mode == ios::app || mode == ios::ate)
  43.     pstart = cp + strlen(cp);
  44.     else
  45.     pstart = cp;
  46.     _strbuf = new strstreambuf(cp, n, pstart);
  47. }
  48.  
  49.  
  50. strstream::strstream() : iostream(NULL)
  51. {
  52.     _strbuf = new strstreambuf;
  53. }
  54.  
  55. strstream::strstream(char *cp, int n, int mode) : iostream(NULL)
  56. {
  57.     char *pstart;
  58.     if (mode == ios::app || mode == ios::ate)
  59.     pstart = cp + strlen(cp);
  60.     else
  61.     pstart = cp;
  62.     _strbuf = new strstreambuf(cp, n, pstart);
  63. }
  64.  
  65.  
  66. char *strstreambuf::str()
  67. {
  68.     freeze(1);
  69.     return _buffer;
  70. }
  71.  
  72. _G_size_t strstreambuf::pcount()
  73. {
  74.     _G_size_t put_len = pptr() - pbase();
  75.     if (put_len < _len) put_len = _len;
  76.     return put_len;
  77. }
  78.  
  79. int strstreambuf::overflow(int c = EOF)
  80. {
  81.   const int flush_only = c == EOF;
  82.   size_t pos = pptr() - pbase();
  83.   size_t get_pos = gptr() - pbase();
  84.   if (pos > _len) _len = pos;
  85.   if (pos >= _size + flush_only) {
  86.       char *new_buf;
  87.       size_t new_size = 2 * _size;
  88.       if (_frozen) /* not allowed to enlarge */
  89.       return EOF;
  90.       else {
  91.       new_buf = ALLOC_BUF(new_size);
  92.       memcpy(new_buf, _buffer, _size);
  93.       }
  94.       if (new_buf == NULL) {
  95. //      __ferror(fp) = 1;
  96.       return EOF;
  97.       }
  98.       _size = new_size;
  99. #if 0
  100.       if (lenp == &_len) /* use '\0'-filling */
  101.       memset(new_buf + pos, 0, _size - pos);
  102. #endif
  103.       _buffer = new_buf;
  104.       setb(new_buf, new_buf + _size, 1);
  105.     }
  106.  
  107.   setp(_buffer, _buffer + _size);
  108.   pbump(pos);
  109.   setg(_buffer, _buffer + get_pos, _buffer + _len);
  110.   if (!flush_only) {
  111.       *pptr() = (unsigned char) c;
  112.       pbump(1);
  113.   }
  114.   return c;
  115. }
  116.  
  117. int strstreambuf::underflow()
  118. {
  119.     size_t ppos = pptr() - pbase();
  120.     if (ppos > _len) _len = ppos;
  121.     setg(_buffer, gptr(), _buffer + _len);
  122.     if (gptr() < egptr())
  123.     return *gptr();
  124.     else
  125.     return EOF;
  126. }
  127.  
  128. strstreambuf::strstreambuf()
  129. {
  130.     _frozen = 0;
  131. //    _flags &= ~ios::dont_close; 
  132.     _len = 0;
  133.     _size = 128;
  134.     _buffer = ALLOC_BUF(_size);
  135.     setb(_buffer, _buffer+_size);
  136.     setp(_buffer, _buffer+_size);
  137.     setg(_buffer, _buffer, _buffer);
  138.     xsetflags(_S_CAN_READ | _S_CAN_WRITE);
  139. }
  140.  
  141. strstreambuf::strstreambuf(int initial)
  142. {
  143.     _frozen = 0;
  144. //    _flags &= ~ios::dont_close; 
  145.     _len = 0;
  146.     if (initial < 16)
  147.     initial = 16;
  148.     _size = initial;
  149.     _buffer = ALLOC_BUF(_size);
  150.     setb(_buffer, _buffer+_size);
  151.     setp(_buffer, _buffer+_size);
  152.     setg(_buffer, _buffer, _buffer);
  153.     xsetflags(_S_CAN_READ | _S_CAN_WRITE);
  154. }
  155.  
  156. strstreambuf::strstreambuf(char *ptr, int size, char *pstart)
  157. {
  158. //    _flags &= ~ios::dont_close; 
  159.     _frozen = 1;
  160.     _len = 0;
  161.     if (size == 0)
  162.     _size = strlen(ptr);
  163.     else if (size < 0) {
  164.     // If size is negative 'the characters are assumed to
  165.     // continue indefinitely.'
  166.     // The following semi-portable kludge tries to do that.
  167.     // It assumes that sizeof(long) == sizeof(pointer).
  168.     // Hence, (unsigned long)(-1) should be the largest
  169.     // possible address.
  170.     unsigned long highest = (unsigned long)(-1);
  171.     // Pointers are signed on some brain-damaged systems, in
  172.     // which case we divide by two to get the maximum signed address.
  173.     if  ((char*)highest < ptr)
  174.         highest >>= 1;
  175.     _size = (char*)highest - ptr;
  176.     }
  177.     else
  178.     _size = size;
  179.     _buffer = ptr;
  180.     int new_flags = _S_CAN_READ;
  181.     setb(_buffer, _buffer+_size);
  182.     if (pstart) {
  183.     new_flags |= _S_CAN_WRITE;
  184.     setp(_buffer, _buffer+_size);
  185.     pbump(pstart-_buffer);
  186.     setg(_buffer, _buffer, pstart);
  187.     }
  188.     else {
  189.     setp(_buffer, _buffer); 
  190.     setg(_buffer, _buffer, _buffer+_size);
  191.     }
  192.     xsetflags(new_flags);
  193. }
  194.  
  195. strstreambuf::~strstreambuf()
  196. {
  197.     if (_frozen == 0)
  198.         FREE_BUF(_buffer);
  199. }
  200.  
  201. virtual streampos strstreambuf::seekoff(streamoff off, _seek_dir dir,
  202.                     int mode /*=ios::in|ios::out*/)
  203. {
  204.     size_t cur_size = pcount();
  205.     streampos new_pos = EOF; 
  206.     if ((mode & ios::in) && (xflags() & _S_CAN_READ)) {
  207.     switch (dir) {
  208.       case ios::end:
  209.         off += cur_size;
  210.         break;
  211.       case ios::cur:
  212.         off += gptr() - pbase();
  213.         break;
  214.       default: /*case ios::beg: */
  215.         break;
  216.     }
  217.     if (off < 0 || (size_t)off > cur_size)
  218.         return (streampos)(EOF);
  219.     setg(_buffer, _buffer + off, _buffer + cur_size);
  220.     new_pos = off;
  221.     }
  222.     if ((mode & ios::out) && (xflags() & _S_CAN_WRITE)) {
  223.     switch (dir) {
  224.       case ios::end:
  225.         off += cur_size;
  226.         break;
  227.       case ios::cur:
  228.         off += pptr() - pbase();
  229.         break;
  230.       default: /*case ios::beg: */
  231.         break;
  232.     }
  233.     if (off < 0 || (size_t)off > cur_size)
  234.         return (streampos)(EOF);
  235.     pbump(_buffer + off - pptr());
  236.     new_pos = off;
  237.     }
  238.     return new_pos;
  239. }
  240.